<?php
namespace app\index\model;
/**
 * Created by Submint Text.
 * User: hetaoren<452649510@qq.com>
 * Date: 2017/03/10
 * Explain: 地区业务处理
 */
class Areas extends Base{
	/**
	 * 分页
	 */
	public function pageQuery(){
		$parentId = input('get.parentId/d',0);
		return $this->where(['dataFlag'=>1,'parentId'=>$parentId])->order('areaId desc')->paginate(input('post.pagesize/d'));
	}
	
	/**
	 * 获取指定对象
	 */
	public function getById($id){
		return $this->get(['dataFlag'=>1,'areaId'=>$id]);
	}
	
	/**
	 * 获取地区
	 */
	public function getFieldsById($id,$fileds){
		return $this->where(['dataFlag'=>1,'areaId'=>$id])->field($fileds)->find();
	}
	
	/**
	 * 迭代获取下级
	 */
	public function getChild($ids = array(),$pids = array()){
		$result = $this->where("dataFlag=1 and parentId in(".implode(',',$pids).")")->select();
		if(count($result)>0){
			$cids = array();
			foreach ($result as $key =>$v){
				$cids[] = $v['areaId'];
			}
			$ids = array_merge($ids,$cids);
			return $this->getChild($ids,$cids);
		}else{
			return $ids;
		}
	}
	
    /**
	 * 根据子分类获取其父级分类
	 */
	public function getParentIs($id,$data = array()){
		$data[] = $id;
		$parentId = $this->where('areaId',$id)->value('parentId');
		if($parentId==0){
			krsort($data);
			return $data;
		}else{
			return $this->getParentIs($parentId, $data);
		}
	}

	/**
	 * 获取自己以及父级的地区名称
	 */
	public function getParentNames($id,$data = array()){
		$areas = $this->where('areaId',$id)->field('parentId,areaName')->find();
		$data[] = $areas['areaName'];
		if((int)$areas['parentId']==0){
			krsort($data);
			return $data;
		}else{
			return $this->getParentNames((int)$areas['parentId'], $data);
		}
	}

	
	/**
	 * 排序字母
	 */
	public function letterObtain(){
		$areaName =  input('code');
		if($areaName =='')return HTRReturn("", 1);
		$areaName = HTRGetFirstCharter($areaName);
		if($areaName){
			return HTRReturn($areaName, 1);
		}else{
			return HTRReturn("", 1);
		}
	}
	
	/**
	 * 新增
	 */
	public function add(){
		$areaType = 0;
		$parentId = input('post.parentId/d',0);
		if($parentId>0){
			$prs = $this->getFieldsById($parentId,['areaType']);
			$areaType = $prs['areaType']+1;
		}
		$data = input('post.');
		HTRUnset($data,'areaId,dataFlag');
		$data['areaType'] = $areaType;
		$data['createTime'] = date('Y-m-d H:i:s');
		$result = $this->validate('Areas.add')->allowField(true)->save($data);
		if(false !== $result){
			$this->createJs();
			return HTRReturn("新增成功", 1);
		}else{
			return HTRReturn($this->getError(),-1);
		}
	}
	
	/**
	 * 编辑
	 */
	public function edit(){
		$areaId = input('post.areaId/d');
		$result = $this->validate('Areas.edit')->allowField(['areaName','areaSort','areaKey'])->save(input('post.'),['areaId'=>$areaId]);
		if(false !== $result){
			$this->createJs();
			return HTRReturn("修改成功", 1);
		}else{
			return HTRReturn($this->getError(),-1);
		}
	}
	
	/**
	 * 删除
	 */
	public function del(){
		$ids = array();
		$ids[] = input('post.id/d');
		$ids = $this->getChild($ids,$ids);
		$data = [];
		$data['dataFlag'] = -1;
		$result = $this->where("areaId in(".implode(',',$ids).")")->update($data);
		if(false !== $result){
			$this->createJs();
			return HTRReturn("删除成功", 1);
		}else{
			return HTRReturn($this->getError(),-1);
		}
	}
	
	/**
	 *  获取地区列表
	 */
	public function listQuery($parentId){
		return $this->where(['dataFlag'=>1,'parentId'=>$parentId])->field('areaId,areaName,parentId')->order('areaSort desc')->select();
	}

	/**
	 *  生成地区js
	 */
	public function createJs(){
		$con = '';
		$iosProvinces = $this->where(['dataFlag'=>1,'areaType'=>0])->field(['areaId,areaName,parentId'])->order('areaSort desc')->select();
		if($iosProvinces){
			$con .= 'var iosProvinces = [';
			foreach ($iosProvinces as $k => $v) {				
				$con .= "{'id': '$v[areaId]', 'value': '$v[areaName]', 'parentId': '$v[parentId]'},";
			}
			$con .= '];';
		}
		$iosCitys = $this->where(['dataFlag'=>1,'areaType'=>1])->field(['areaId,areaName,parentId'])->order('areaSort desc')->select();
		if($iosCitys){
			$con .= 'var iosCitys = [';
			foreach ($iosCitys as $k => $v) {				
				$con .= "{'id': '$v[areaId]', 'value': '$v[areaName]', 'parentId': '$v[parentId]'},";
			}
			$con .= '];';
		}
		$iosCountys = $this->where(['dataFlag'=>1,'areaType'=>2])->field(['areaId,areaName,parentId'])->order('areaSort desc')->select();
		if($iosCountys){
			$con .= 'var iosCountys = [';
			foreach ($iosCountys as $k => $v) {				
				$con .= "{'id': '$v[areaId]', 'value': '$v[areaName]', 'parentId': '$v[parentId]'},";
			}
			$con .= '];';
		}
		@file_put_contents('static/js/city.js',$con);
	}
	/**
	 * 获取地区名称
	 */
	public function getNames($id){
		return $this->where('areaId',$id)->value('areaName');
	}
}